<?php
require 'authorisation.php';

$traPk=intval($_REQUEST['traPk']);

$link = db_connect();
$sql = "select comPk from tblComTaskTrack where traPk=$traPk";
$result = mysql_query($sql, $link) or die("can't get associated comp");
$comPk = mysql_result($result,0,0);
$usePk = check_auth('system');

$link = db_connect();
$isadmin = is_admin('admin',$usePk,$comPk);

if (1)
{
    // implement a nice text file output
    #G  WGS 84
    #U  1
    #W  1D-027 A 36.4402500000ºS 146.3607333333ºE 27-MAR-62 00:00:00 885.000000 WHITF RD/SNO
    #W  1G-020 A 36.5787000000ºS 146.3786833333ºE 27-MAR-62 00:00:00 656.000000 MOYHU
    #W  6S-034 A 36.7462000000ºS 146.9788500000ºE 27-MAR-62 00:00:00 1115.000000 MYSTIC LZ
    # etc.

    $sql = "SELECT date_format(T.traStart,'%d%m%y'), P.pilFirstName, P.pilLastName, T.traDate from tblTrack T, tblPilot P where T.traPk=$traPk and T.pilPk=P.pilPk";
    $result = mysql_query($sql,$link) or die("Unable to find track: " . mysql_error() . "\n");

    $date = mysql_result($result, 0, 0);
    $pilot = mysql_result($result, 0, 1) . ' ' . mysql_result($result, 0, 2);
    $ndate = mysql_result($result, 0, 3);

    header("Content-type: text/igc");
    header("Cache-Control: no-store, no-cache");
    header("Content-Disposition: attachment; filename=\"$ndate-$pilot.igc\"");
    # nuke normal header ..
    #AXXXZZZGPSBabel
    #HFDTE040207
    #HFPLTPILOT:Unknown

    print "AXXXZZZairScore\r\n";
    print "HFDTE$date\r\n";
    print "HFPLTPILOT: $pilot\r\n";
    print "HFDTM100DATUM: WGS-1984\r\n";

    $sql = "SELECT TL.* from tblTrackLog TL where TL.traPk=$traPk order by TL.trlTime";
    $result = mysql_query($sql,$link) or die("Unable to find tracklog: " . mysql_error() . "\n");

    # B0442223707362S14524802EA0000000650
    $cross = 0;
    while($row = mysql_fetch_array($result))
    {
        $time = 0 + $row['trlTime'];
        $h = floor($time / 3600);
        if ($cross == 0 && $h > 24)
        {
            $cross = 1;
            // FIX: output a DTE again?
        }
        $m = floor(($time - $h * 3600)/60);
        $s = $time % 60;
        $ftime = sprintf("%02d%02d%02d",($h%24),$m,$s);

        $lat = floatval($row['trlLatDecimal']);
        if ($lat < 0)
        {
            $h = floor(abs($lat));
            $alat = round((abs($lat)-$h)*60000,0);
            $slat = sprintf("%02d%05dS",$h, $alat);
        }
        else
        {
            $h = floor($lat);
            $alat = round(($lat-$h)*60000,0);
            $slat = sprintf("%02d%05dN",$h, $alat);
        }
        $lon = floatval($row['trlLongDecimal']);
        if ($lon < 0)
        {
            $h = floor(abs($lon));
            $alon = round((abs($lon)-$h)*60000,0);
            $slon = sprintf("%03d%05dW",$h, $alon);
        }
        else
        {
            $h = floor($lon);
            $alon = round(($lon-$h)*60000,0);
            $slon = sprintf("%03d%05dE",$h, $alon);
        }
        // FIX: add airspace check and return '0' alt if track
        // is infringing ...
        $altv = 0 + $row['trlAltitude'];
        if ($altv > 2500)
        {
            $altv = 2500;
        }
        $alt = sprintf("%05d", $altv);
        # convert lat/lon to NESW appropriately
        # get some decent date ..
        print "B${ftime}${slat}${slon}A00000${alt}\r\n";
    }
    # Footer
    #LXXXGenerated by GPSBabel Version 1.3.0
    #GGPSBabelSecurityRecordGuaranteedToFailVALIChecks
    print "LXXXGenerated by airScore 0.1\r\n";
    print "GFailed security check\r\n";
}
else
{
    print "Only administrators may download tracks at this time<br>\r\n";
}
?>
